import 'package:cached_network_image/cached_network_image.dart';
import 'package:du_application/model/OrderBuyModel.dart';
import 'package:du_application/provide/OrderProvider.dart';
import 'package:du_application/routes/application.dart';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:provide/provide.dart';

class Order extends StatefulWidget {
  Order({Key key}) : super(key: key);

  @override
  _OrderState createState() => _OrderState();
}

class _OrderState extends State<Order> with TickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    Provide.value<OrderProvider>(context).getOrder(context);
    TabController tabController =
        TabController(initialIndex: 0, length: 3, vsync: this);

    return Provide<OrderProvider>(
      builder: (context, child, index) {
        return index == null || index.orderBuyModel == null
            ? _loadingContainer
            : Scaffold(
                appBar: AppBar(
                  title: Text("我的购买"),
                  bottom: TabBar(
                    controller: tabController,
                    tabs: <Widget>[
                      Tab(
                        child: Text("待付款"),
                      ),
                      Tab(
                        child: Text("进行中"),
                      ),
                      Tab(
                        child: Text("历史订单"),
                      ),
                    ],
                  ),
                ),
                body: TabBarView(
                  controller: tabController,
                  children: <Widget>[
                    buyBefore(index.orderBuyModel.data.order.unpaid, context),
                    buyBefore(index.orderBuyModel.data.order.unship, context),
                    buyBefore(index.orderBuyModel.data.order.uncomment,context),
                  ],
                ),
              );
      },
    );
  }

  final _loadingContainer = Container(
      width: ScreenUtil().setHeight(1080),
      height: ScreenUtil().setHeight(1920),
      constraints: BoxConstraints.expand(),
      color: Colors.black,
      child: Center(
        child: Opacity(
          opacity: 0.9,
          child: SpinKitWave(
            color: Colors.white,
            size: 50.0,
          ),
        ),
      ));
}

Widget buyBefore(List<Uncomment> orders, context) {
  ScrollController scrollController = ScrollController();
  Widget _getButtom(order, context) {
    switch (order.orderStatus) {
      case 101:
        return Container(
          alignment: Alignment.center,
          width: ScreenUtil().setWidth(160),
          height: ScreenUtil().setWidth(70),
          child: Text(
            "去付款",
            style: TextStyle(color: Colors.white),
          ),
          decoration: BoxDecoration(
            color: Colors.cyan[400],
            borderRadius: BorderRadius.circular(ScreenUtil().setWidth(10)),
          ),
        );
        break;
      case 401:
        return InkWell(
            onTap: () {
              showModalBottomSheet(
                  context: context,
                  builder: (context) {
                    return Provide<OrderProvider>(
      builder: (context, child, index) {
     return   Stack(
          children: <Widget>[
Container( child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(
                              width: ScreenUtil().setWidth(1080),
                              padding: EdgeInsets.symmetric(
                                  vertical: ScreenUtil().setSp(30)),
                              height: ScreenUtil().setWidth(100),
                              child: Text("评价订单"),
                              decoration: BoxDecoration(
                                  border: Border(
                                      bottom: BorderSide(
                                          color: Colors.grey[200],
                                          width: ScreenUtil().setSp(1)))),
                            ),
                          Container(
                            margin: EdgeInsets.only(top:ScreenUtil().setSp(10)),
                            child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceEvenly,
                                children: [
                                  InkWell(
                                      onTap: () {
                                       index.replaceSize(1);
                                      },
                                      child: Container(
                                        padding: EdgeInsets.symmetric(
                                            horizontal: 10, vertical: 5),
                                        child: Text(
                                          "很赞",
                                          style: TextStyle(color: Colors.white),
                                        ),
                                        decoration: BoxDecoration(
                                          color: index.size==1?Colors.cyan[400]:Colors.grey[400],
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(3)),
                                          // border: Border.all(width:1,color:Colors.grey),
                                        ),
                                      )),
                                  InkWell(
                                      onTap: () {
                                            index.replaceSize(2);
                                      },
                                      child: Container(
                                        padding: EdgeInsets.symmetric(
                                            horizontal: 10, vertical: 5),
                                        child: Text(
                                          "偏大",
                                          style: TextStyle(color: Colors.white),
                                        ),
                                        decoration: BoxDecoration(
                                          color: index.size==2?Colors.cyan[400]:Colors.grey[400],
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(3)),
                                          // border: Border.all(width:1,color:Colors.grey),
                                        ),
                                      )),
                                  InkWell(
                                      onTap: () {
                                    index.replaceSize(3);
                                      },
                                      child: Container(
                                        padding: EdgeInsets.symmetric(
                                            horizontal: 10, vertical: 5),
                                        child: Text(
                                          "偏小",
                                          style: TextStyle(color: Colors.white),
                                        ),
                                        decoration: BoxDecoration(
                                          color: index.size==3?Colors.cyan[400]:Colors.grey[400],
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(3)),
                                          // border: Border.all(width:1,color:Colors.grey),
                                        ),
                                      )),
                                ])
                          ),
                                Container(
                                  margin: EdgeInsets.symmetric(vertical: ScreenUtil().setSp(20)),
                                  child:TextField(
                                    onChanged: (value){
                                       index.replaceContent(value);
                                    },
                                        decoration: InputDecoration(
                                        border: InputBorder.none,
                                        labelStyle:TextStyle(
                                          color:Colors.black
                                        ) ,
                                        labelText: '评论',
                                        hintText: '评论',
                                        focusColor: Colors.black,
                                        focusedBorder: InputBorder.none,
                                        ),
                                  )
                                )
                          ]),
                    ),
                         Positioned(
              right: ScreenUtil().setWidth(40),
              bottom: ScreenUtil().setHeight(40),
              child: InkWell(
                  onTap: () async {
               bool result= await  index.comment(context,order.goods.id);
               if(result){
               
                Application.router.navigateTo(context, "/order",replace: true);
               }
                  
                  },
                  child: Container(
                      padding: EdgeInsets.symmetric(
                          horizontal: ScreenUtil().setSp(40),
                          vertical: ScreenUtil().setSp(20)),
                      color: Colors.cyan[400],
                      margin: EdgeInsets.only(left: ScreenUtil().setSp(60)),
                      child: Text(
                        "确认",
                        style: TextStyle(color: Colors.white),
                      ))))
          ],
        );
     
      });
      });
     },
            child: Container(
              alignment: Alignment.center,
              width: ScreenUtil().setWidth(160),
              height: ScreenUtil().setWidth(70),
              child: Text(
               "评价",
              
                style: TextStyle(color: Colors.white),
              ),
              decoration: BoxDecoration(
                color: Colors.cyan[400],
                borderRadius: BorderRadius.circular(ScreenUtil().setWidth(10)),
              ),
            ));
        break;
      default:
        return Text("${order.text}");
    }
  }

  return ListView(
      controller: scrollController,
      children: orders.map((order) {
        return Card(
          child: Container(
              padding:
                  EdgeInsets.symmetric(vertical: ScreenUtil().setWidth(15)),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    width: ScreenUtil().setWidth(1080),
                    padding: EdgeInsets.symmetric(
                        horizontal: ScreenUtil().setWidth(15),
                        vertical: ScreenUtil().setWidth(25)),
                    child: Text(
                      "订单号:${order.orderSn}",
                      textAlign: TextAlign.start,
                    ),
                    decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(
                                color: Colors.grey[200],
                                width: ScreenUtil().setSp(1)))),
                  ),
                  Row(
                    children: <Widget>[
                      Container(
                        width: ScreenUtil().setWidth(200),
                        height: ScreenUtil().setHeight(200),
                        padding: EdgeInsets.all(ScreenUtil().setSp(10)),
                        child: CachedNetworkImage(
                          height: ScreenUtil().setWidth(200),
                          width: ScreenUtil().setWidth(200),
                          imageUrl: order.goods.picUrl,
                          fit: BoxFit.fitWidth,
                          placeholder: (context, url) =>
                              Image.asset('assets/loading.jpg'),
                        ),
                      ),
                      Stack(
                        children: <Widget>[
                          Positioned(
                            right: ScreenUtil().setWidth(20),
                            bottom: ScreenUtil().setHeight(1),
                            child: _getButtom(order, context),
                          ),
                          Column(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Container(
                                width: ScreenUtil().setWidth(850),
                                child: Text(order.goods.goodsName, maxLines: 2),
                              ),
                              Container(
                                  margin: EdgeInsets.only(
                                      top: ScreenUtil().setSp(10),
                                      bottom: ScreenUtil().setSp(10)),
                                  height: ScreenUtil().setHeight(50),
                                  child:
                                      Text(order.goods.specifications.join())),
                              Row(
                                children: <Widget>[
                                  Text(
                                    "￥",
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        fontSize: ScreenUtil().setSp(20)),
                                  ),
                                  Text(order.goodsPrice.toString(),
                                      style: TextStyle(
                                          fontWeight: FontWeight.bold,
                                          fontSize: ScreenUtil().setSp(30))),
                                ],
                              )
                            ],
                          )
                        ],
                      )
                    ],
                  )
                ],
              )),
        );
      }).toList());
}
